package com.huike.service.impl.sysImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huike.Position.dtos.PositionDto;
import com.huike.Position.dtos.PositionPageDTO;
import com.huike.Position.pojos.Position;
import com.huike.Position.vos.PositionVO;
import com.huike.constans.SysConstantsMessage;
import com.huike.mapper.sys.PositionMapper;
import com.huike.result.PageResult;
import com.huike.service.sys.PositionService;
import com.huike.utils.PinyinUtil;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class PositionServiceImpl implements PositionService {

    @Autowired
    private PositionMapper positionMapper;

    //条件分页查询岗位数据
    @Override
    public PageResult pageList(PositionPageDTO pageDTO) {
        //验空
        if (pageDTO == null) throw new RuntimeException(SysConstantsMessage.NOT_PARAMETERS);
        pageDTO.initialize();
        //组装分页参数
        IPage<Position> page = new Page<>(pageDTO.getPage(), pageDTO.getPageSize());
        LambdaQueryWrapper<Position> lqw = new LambdaQueryWrapper<>();
        lqw.eq(pageDTO.getStatus() != null, Position::getStatus, pageDTO.getStatus());
        lqw.like(pageDTO.getPositionName() != null && !"".equals(pageDTO.getPositionName()), Position::getPositionName, pageDTO.getPositionName());
        lqw.like(pageDTO.getPositionNumber() != null && !"".equals(pageDTO.getPositionNumber()), Position::getPositionNumber, pageDTO.getPositionNumber());


        positionMapper.selectPage(page, lqw);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getRecords());
        return pageResult;
    }

    //添加岗位
    @Override
    public void insertPosition(PositionDto dto) {
        //参数校验
        if (dto == null) throw new RuntimeException(SysConstantsMessage.NOT_PARAMETERS);
        Position position = new Position();
        BeanUtils.copyProperties(dto,position);
        if (dto.getPositionName() == null) throw new RuntimeException(SysConstantsMessage.NOT_PARAMETERS);
        //岗位验证唯一
        Integer id = positionMapper.getByName(position);
        if (id != null) throw new RuntimeException("岗位已存在");
        //岗位编码唯一
       if (dto.getPositionNumber() == null) throw new RuntimeException(SysConstantsMessage.POSITION_NAME_IS_TWO);
         id = positionMapper.getByNumber(position);
         if (id != null) throw new RuntimeException("岗位编码已存在");

        //生成拼音助记码
       // String numberString = PinyinUtil.getFirstLetters(dto.getPositionName(), HanyuPinyinCaseType.UPPERCASE);
       // position.setPositionNumber(numberString);
        position.setCreateUser(1);
        position.setUpdateUser(1);
        position.setBranchId(1);
        position.setCreateTime(LocalDateTime.now());
        position.setUpdateTime(LocalDateTime.now());
        position.setSort(1);

        positionMapper.insertPosition(position);
    }


    @Override
    public PositionVO getById(Integer id) {
        if (id == null)throw new RuntimeException(SysConstantsMessage.NOT_PARAMETERS);
        Position position = positionMapper.getById(id);
        PositionVO positionVO = new PositionVO();
        BeanUtils.copyProperties(position,positionVO);
        positionVO.setStatus(position.getStatus().toString());
        return positionVO;
    }


    @Override
    public void deleteById(List<Integer> ids) {
        if (ids == null)throw new RuntimeException(SysConstantsMessage.NOT_PARAMETERS);

        positionMapper.deletePosition(ids);
    }


    @Override
    public void updatePosition(Position dto) {

        //参数校验
        if (dto == null) throw new RuntimeException(SysConstantsMessage.NOT_PARAMETERS);
        if (dto.getPositionName() == null) throw new RuntimeException(SysConstantsMessage.NOT_PARAMETERS);
        //岗位验证唯一
        Integer id = positionMapper.getByName(dto);
        if (id != null) throw new RuntimeException("岗位编码已存在");
        //岗位编码唯一
        if (dto.getPositionNumber() == null) throw new RuntimeException(SysConstantsMessage.POSITION_NAME_IS_TWO);
        id = positionMapper.getByNumber(dto);
        if (id != null) throw new RuntimeException("岗位已存在");

        positionMapper.updatePosition(dto);


    }
}
